﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.Windows.Forms;
using System.IO;
using System.Drawing;
using System.Xml.Serialization;

namespace xmlTools
{
    public class XMLClass
    {
        private static string defultpath = Application.StartupPath;
        private static string file = string.Empty;

        private static string XMLFileName
        {
            set { file = value; }
            get { return file; }
        }
        /// <summary>
        /// 泛型序列化写入文本
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="OBJECT"></param>
        /// <returns></returns>
        public static bool XmlSerialize<T>(ref T OBJECT,string rootFileName ,string XMLFileName)
        {
            string path = defultpath +"\\"+ rootFileName+"\\"+ XMLFileName;
            XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
            ns.Add("", "");
            try
            {
                //如果文件目录不存在，那么创建目录
                if (!File.Exists(path))
                {
                    FileInfo fi = new FileInfo(path);
                    if (!fi.Directory.Exists)
                    {
                        Directory.CreateDirectory(fi.Directory.FullName);
                    }
                }

                Type type = OBJECT.GetType();
                using (Stream stream = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.None))
                {
                    XmlSerializer format = new XmlSerializer(type);
                    format.Serialize(stream, OBJECT, ns);
                    stream.Close();
                }
                return true;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return false;
            }

        }

        /// <summary>
        /// 泛型 xml反序列化
        /// </summary>
        /// <param name="path"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        public static T XmlDeserialize<T>(string rootFileName,string XMLFileName)
        {
            string path = rootFileName;// defultpath + "\\" + rootFileName + "\\" + XMLFileName;
            Type type = typeof(T);
            T obj = default(T);
            if (!File.Exists(path))
            {
                return obj;
            }

            try
            {
                using (Stream stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    XmlSerializer formatter = new XmlSerializer(type);
                    stream.Seek(0, SeekOrigin.Begin);
                    obj = (T)formatter.Deserialize(stream);
                    stream.Close();
                    return obj;
                }
            }
            catch
            {
                return obj;
            }
        }
    #if false
        public static T XmlDeserialize<T>(string xmlstr)
        {

           // string path = defultpath + "\\" + rootFileName + "\\" + XMLFileName;
            Type type = typeof(T);
            T obj = default(T);

            if (!File.Exists(path))
            {
                return obj;
            }

            try
            {
                using (Stream stream = new FileStream
                    XmlSerializer formatter = new Xml(path, FileMode.Open, FileAccess.Read, FileShare.Read))
                {   Serializer(type);
                    stream.Seek(0, SeekOrigin.Begin);
                    obj = (T)formatter.Deserialize(stream);
                    stream.Close();
                    return obj;
                }
            }
            catch
            {
                return obj;
            }

        } 
        #endif
    }
}
